package svgedit.gui.commands;

import java.awt.Point;

import svgedit.gui.manipulators.MoveManipulator.MoveElementVisitor;
import svgedit.svg.SVGElement;

/**
 * Handles the moving of a shape(s) around the document
 * @author SARJ
 *
 */
public class MoveCommand implements Command
{
	// The element
	private SVGElement element;

	// Starting and ending points of the shape moving
	private Point startPoint;
	private Point endPoint;

	// Visitors that will move the shape around
	private MoveElementVisitor startVisitor;
	private MoveElementVisitor endVisitor;

	/**
	 * Constructor that prepares the move command
	 * @param element The element to move
	 * @param startPoint The point at which the element started at
	 */
	public MoveCommand(SVGElement element, Point startPoint)
	{
		this.element = element;
		this.startPoint = new Point(startPoint);
	}

	@Override
	public void executeUndo()
	{
		// Move the element back
		element.acceptVisitor(startVisitor);

	}

	@Override
	public void executeRedo()
	{
		// Move the element forward
		element.acceptVisitor(endVisitor);
	}

	/**
	 * Defines the end position, used for setting up the visitors so they
	 * move the element correctly.
	 * @param endPoint The end point of the shape (mouse release)
	 */
	public void setEndPosistion(Point endPoint)
	{
		this.endPoint = new Point(endPoint);

		// Create a move visitor that will bring the element to its start pos
		startVisitor = new MoveElementVisitor(
				(int) (startPoint.getX() - endPoint.getX() ),
				(int) (startPoint.getY() - endPoint.getY()));

		// Create a move visitor that will bring the element to its end pos
		endVisitor = new MoveElementVisitor(
				(int) (endPoint.getX() - startPoint.getX()),
				(int) (endPoint.getY() - startPoint.getY()));

	}
	

}
